package com.egene.tw.push;

import android.content.Context;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.egene.tw.api.EgeneApi;
import com.egene.tw.api.LoginApi;
import com.egene.tw.dao.UserDao;
import com.egene.tw.network.ApiHttpClient;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.FeedbackCmdMessage;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.igexin.sdk.message.SetTagCmdMessage;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EService;
import org.apache.http.Header;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
@EService
public class PushIntentService extends GTIntentService {
    public static String CLIENTID;

    @Bean
    UserDao userDao;

    public PushIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        CLIENTID = clientid;
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
        if (userDao.getUser() == null) {
            return;
        }
        if(TextUtils.isEmpty(userDao.getUser().getUserID())){
            return;
        }

        SyncHttpClient client = new SyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("cmd", "ServerHandler.GeTuiClientID");
        params.put("userId", userDao.getUser().getUserID());
        params.put("clientid", clientid);
        System.out.println(new StringBuilder("").append(LoginApi.API_URL).append("?").append(params).toString());
        client.post(LoginApi.API_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                System.out.println("updateGetuiClient onSuccess");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                System.out.println("updateGetuiClient onFailure");
            }
        });

    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }
}